package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.ModelPrompt;
import org.dromara.dao.business.domain.ModelPromptQuery;
import org.dromara.dao.business.mapper.ModelPromptMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class ModelPromptService {

    @Autowired
    ModelPromptMapper modelPromptMapper;


    public TableDataInfo<ModelPrompt> queryPageList(ModelPromptQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                modelPromptMapper.paginationSelectList(query, pageQuery),
                modelPromptMapper.paginationSelectCount(query)
        );
    }


    public ModelPrompt queryById(Long id) {
        return modelPromptMapper.selectById(id);
    }


    @Transactional
    public ModelPrompt insert(ModelPrompt entity) {
        modelPromptMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(ModelPrompt update) {

        ModelPrompt entity = modelPromptMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setModelId(update.getModelId());
        entity.setModelName(update.getModelName());
        entity.setPrompt(update.getPrompt());
        entity.setUpdateTime(LocalDateTime.now());
        modelPromptMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        modelPromptMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        modelPromptMapper.deleteByIds(Arrays.asList(ids));
    }


    public List<ModelPrompt> getPrompt() {
        return modelPromptMapper.selectList(new LambdaQueryWrapper<>());
    }
}
